using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SMS.Framework;

namespace SMS.TrayWindow
{
    public class TrayWindow : ITrayWindow
    {
        private TrayForm _TrayWindowInstance;

        public TrayWindow()
        {
            Context.Current.OnApplicationCommand +=new OnApplicationCommandEventHandler(OnApplicationCommand);
        }

        #region ITrayWindow Members

        public void SetMenu(string name, bool enable)
        {
            if (!Init()) return;
            foreach (MenuItem mi in _TrayWindowInstance.NotifyIcon.ContextMenu.MenuItems)
            {
                if (mi.Name.ToString() == name)
                {
                    mi.Enabled = enable;
                    return;
                }
            }
        }

        public void SetMenu(string name, string value)
        {
            if (!Init()) return;
            foreach (MenuItem mi in _TrayWindowInstance.NotifyIcon.ContextMenu.MenuItems)
            {
                if (mi.Name.ToString() == name)
                {
                    if (value != null) mi.Text = value;
                    return;
                }
            }
        }

        public void SetMenu(string name, string value, bool enable)
        {
            if (!Init()) return;
            foreach (MenuItem mi in _TrayWindowInstance.NotifyIcon.ContextMenu.MenuItems)
            {
                if (mi.Name.ToString() == name)
                {
                    if (value != null) mi.Text = value;
                    mi.Enabled = enable;
                    return;
                }
            }
        }

        public void ShowPopUpMessage(string caption, string text, ToolTipIcon icon)
        {
            try { _TrayWindowInstance.NotifyIcon.ShowBalloonTip(1000, caption, text, icon);}
            catch { }
        }

        #endregion

        public bool RefreshMenu()
        {
            if (!Init()) return false;
            _TrayWindowInstance.AddMenus();
            return true;
        }

        #region IWindow Members

        public void CloseWindow()
        {
            
        }

        public bool Init()
        {
            try
            {
                if (_TrayWindowInstance == null || _TrayWindowInstance.IsDisposed) _TrayWindowInstance = new TrayForm();
                return true;
            }
            catch
            { return false; }
        }

        public bool OnApplicationCommand(object sender, string commandName, object[] commandParameters)
        {
            switch (commandName.Trim().ToLower())
            {
                case "showtraywindow":
                    ShowWindow();
                    break;
                case "refreshmenu":
                    RefreshMenu();
                    break;
            }
            return false;
        }

        public void ShowWindow()
        {
            if (!Init()) return;
            try
            {
                _TrayWindowInstance.Show();
            }
            catch
            {
            }
        }

        public void ChangeIcon(TrayIconType iconType)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
